#ifndef TCP_BRIDGE_H__
#define TCP_BRIDGE_H__

#include "rclcpp/rclcpp.hpp"
#include <tcp_server.h>
#include "agv_msgs_converter.h"
#include "ros_json_converter.h"
#include <map>
#include <jsoncpp/json/json.h>
#include "Subscriber.h"
#include "ServiceOperate.h"

class CGetAgvStates
{
public:
    CGetAgvStates(std::shared_ptr<rclcpp::Node> nh);
    ~CGetAgvStates();
    void setSendDataProcessor(std::function<void(std::vector<int>, std::string)> processor);

    void recvData(const int id, std::string &buffer);
    void clientConnStateChanged(const int id, const bool state);

private:
    bool m_cGetAgvStatesInitOk = false;

    std::shared_ptr<rclcpp::Node> m_nh;
    std::vector<rclcpp::SubscriptionBase::SharedPtr> m_subscribers;

    CServiceOperate *m_srvOperator = nullptr;

    // 发送、接收消息
    std::map<std::string, CSubscriberInfo> m_sendData;
    std::map<int, std::string> m_recvMap; // 接收数组

    // 回调
    std::function<void(std::vector<int>, std::string)> m_vSendDataProcessor;

    // 初始化
    bool init();
    template <typename T>
    void subscriberInit(const int index, const std::string &topic);

    bool clearInvalidData(std::string &str);
    bool convertJsonValue(const int id, const std::string &str);
    bool IsJsonIllegal(const char *jsoncontent);
    void parseJsonValue(const int id, const Json::Value &root);

    std::vector<int> getSendIdlist(const std::string &topic);
    void sendJsonData(const std::string &topic, const Json::Value &json_msg, const std::vector<int> &ids);

    template <typename T>
    void subCallback(const T &msg, const std::string &topic);
};

#endif